﻿using System;
using System.Collections.Generic;
using System.Text;
using PostSharp.Aspects.Dependencies;

namespace Arcana.Contract.Property
{
    [Serializable]
    [ProvideAspectRole(Roles.Contract)]
    [ProvideAspectRole(Roles.Property)]
    [ProvideAspectRole(Roles.Conditional)]
    [AspectRoleDependency(AspectDependencyAction.Commute, Roles.Conditional)]
    public sealed class HasAttribute : ComparingContractBase
    {
        private object _value;

        public HasAttribute(object value)
        {
            _value = value;
        }

        protected override bool ValidateProperty(object property)
        {
            if (property.GetType().GetInterface("IEnumerable") == null)
                throw new ArgumentException();
            if (property == null)
                throw new ArgumentNullException();
            System.Collections.IEnumerable _property = (System.Collections.IEnumerable)property;
            foreach (var collectionItem in _property)
            {
                if (Comparer.Compare(collectionItem, _value) == 0) return true;
            }
            return false;
        }
    }
}
